/**
 * Project: lionbule-other
 * 
 * File Created at 2012-4-7
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com;

import java.net.URL;
import java.net.URLClassLoader;

/**
 * TestClassLoader.
 * @author LionBule
 *  2012-4-7 下午2:03:41
 */
public class TestClassLoader extends URLClassLoader{

    public TestClassLoader(URL[] urls) {
        super(urls);
    }

    @Override
    public final Class<?> loadClass(String name) throws ClassNotFoundException {
        return loadClass(name, false);
    }
    
    @Override
    public synchronized Class<?> loadClass(String name, boolean resolve)
            throws ClassNotFoundException {
        Class<?> clazz = null;
        
        if(name.startsWith("lb.test.TestLionbule")){
            clazz = findLoadedClass(name);
            if (clazz != null) {
                if (resolve)
                    resolveClass(clazz);
                return (clazz);
            }

            clazz = findClass(name);
            if (clazz != null) {
                if (resolve)
                    resolveClass(clazz);
                return (clazz);
            }           
        }

        ClassLoader loader = this.getParent();
        if (loader != null) {
            clazz = loader.loadClass(name);
            if (clazz != null) {
                if (resolve)
                    resolveClass(clazz);
                return (clazz);
            }
        }
        throw new ClassNotFoundException(name);
    }

    @Override
    public URL getResource(String name) {
        URL url = findResource(name);
        if (url == null) {
            ClassLoader parent = this.getParent();
            if (parent != null) {
                url = parent.getResource(name);
            } else {
                url = getSystemResource(name);
            }
        }
        return url;
    }
    
}
